const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const autoprefixer = require('autoprefixer');
const TerserPlugin = require('terser-webpack-plugin')

module.exports = {
	//入口文件
    entry: {
        main: './main.js',
    },
    //输入文件名
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: "[name].[chunkhash:8].bundle.js"
    },
    //环境，development：开发环境
    mode: 'production',
    resolve: {
    	//配置目录别名
        alias: {
            vue$: 'vue/dist/vue.esm.js',
            '@assets': path.resolve('./src/assets'),
            '@views': path.resolve('./src/views'),
            '@utils': path.resolve('./src/utils'),
            '@components': path.resolve('./src/components'),
        },
        //配置文件扩展名，这样引入的时候后面就不用加.vue、.js等后缀名了
        extensions: ['*', '.js', '.vue', '.json']
    },
    //配置服务器
    devServer: {
    	//是否关闭用于DNS重绑定的http请求的host检查
        disableHostCheck: true,
        //配置代理
        proxy: {
          //'http://106.53.151.120:8888/user-info/
          '/api' : {
          	//目标请求地址
            target: 'http://132.232.223.165:8888/',
            //是否在请求头中加入host首部
            changeOrigin: false,
            //检测是否是http是否是s
            secure:false,
            //正则匹配路径
            pathRewrite: { '^/api': '' }
          }
        }
    },
    module: {
    	//验证规则
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        useBuiltIns: 'usage',
                                        "corejs": 3,
                                        "targets": {
                                        	//兼容浏览器最后两个版本的，小于等于ie9版本的
                                            "browsers": [
                                                "last 2 versions",
                                                "not ie <= 9"
                                            ]
                                        },
                                    }
                                ]
                            ],
                            "plugins": [
                            	// `style: true` 会加载 less 文件
                                ["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": 'true' }]
                            ]
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: [autoprefixer(['last 2 version', '> 1%', 'not ie < 11'])]
                        }
                    }
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 3 * 1024,
                        outputPath: 'images',// 将文件打包到哪里
                        publicPath: 'images/',
                    }
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
        	//引用的模板文件路径
            template: './public/index.html',
            filename: 'index.html'
        })
    ],
    optimization: {
        minimizer: [
            new TerserPlugin({
            	//是否缓存
                cache: true,
                //是否定位源文件的代码
                sourceMap: true,
                //提取注释文件到txt
                extractComments: false,
                terserOptions: {
                    comments: false,
                    //打包的时候不打包用不到的、console.log的、debug调试的、死代码等代码
                    compress: {
                        unused: true,
                        drop_console: true,
                        drop_debugger: true,
                        dead_code: true
                    }
                },
                //是否多进程并行压缩代码
                parallel: true,
            })
        ]
    }
    
}
